<?
layout("/inc/win_layout.html",{title:sysTitle}){
?>
	<link rel="stylesheet" href="static/uploadify/uploadify.css" type="text/css"></link>
	<script type="text/javascript" src="static/uploadify/jquery.uploadify.min.js"></script>
    <script type="text/javascript" src="static/utf8-jsp/ueditor.config.js"></script>
	<script type="text/javascript" src="static/utf8-jsp/ueditor.all.js"></script>
	<script type="text/javascript" src="static/utf8-jsp/lang/zh-cn/zh-cn.js"></script>
	<script type="text/javascript" src="static/utf8-jsp/third-party/video-js/video.js"></script>
	<script type="text/javascript" src="static/My97DatePicker/WdatePicker.js"></script>
		<div id="loading" style="display: none;">
			<div style="margin-left:40%;margin-top:20%;">
				<img alt="" src="static/vendors/pageloader/images/loader12.GIF" >
				<div>正在导入......</div>
			</div>
		</div>
		<div id="content">
			<div class="col-md-6"  style="margin-left:5%;margin-right:5%;">
             	<div class="form-group">
                	<label for="inputStreet" class="control-label">上传excel文件 <span class='require'>*</span> </label>
            		<input id="uploadFile" type="file"  placeholder="" class="form-control" accept=".xls,.xlsx" onclick=""><i class="alert alert-hide">请上传excel文件！</i>
            		<div class="form-group">
              			<input id="btnUpload" type="submit" name="submit" class="btn btn-success" value="上传"/>
               			<input id="filePath" type="hidden" value="" class="form-control" />
             			<div role="progressbar" id="progressbar" aria-valuetransitiongoal="1" class="progress-bar progress-bar-success" aria-valuenow="1" style="width: 0%;"></div>
            		</div>
                 </div>
             </div>
             <div class="col-md-6"  style="margin-left:5%;margin-right:5%;">
                <div class="form-group">
                    <label for="inputStreet" class="control-label">sheet名称 <span class='require'>*</span></label>
                        <select id="sheetName" class="form-control">
                        </select>
                      </div>
                  </div>
			</div>


<?} ?>
<script>
function getQueryString(name) { 
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
	var r = window.location.search.substr(1).match(reg); 
	if (r != null) return unescape(r[2]); return null; 
	} 
 var winName = getQueryString("winName")
 var index1 = getQueryString("memberId")
 var userType = getQueryString("userType")
$(function(){
	upload();
});

//文件上传

function upload(){
	//默认上传按钮不可用
	$("#btnUpload").attr("disabled", true);

	//上传按钮点击事件
	$("#btnUpload").click(function() {
		//上传按钮修改为可用
		$(this).attr("disabled", true);
		uploadFunction();
	});
	
	// 文件修改时
	$("#uploadFile").change(function() {
		$("#progressbar").width(0);
		$("#progressbar").height(0);
		$("#progressbar").text("");
		$("#btnUpload").show();
		$("#btnUpload").val("上传");
		var file = $(this).prop("files");
		if (file.length != 0) {
			$("#btnUpload").attr("disabled", false);
		}
	});
}

//文件上传
function uploadFunction() {
	var uploadFile = $("#uploadFile").get(0).files[0]; //获取文件对象
	// FormData 对象
	var form = new FormData();
	form.append("file", uploadFile); // 文件对象
	$.ajax({
		cache : false,
		type : "POST",
		url : "./DataHandlingImportUpload.do",
		contentType : false,
		//contentType设置为false。因为是由<form>表单构造的FormData对象，且已经声明了属性enctype="multipart/form-data"
		//如果不是用<form>表单构造FormData对象,append()的第二个参数应是文件对象，即$('#file')[0].files[0],contentType也要设置为‘false’。
		processData : false,//processData设置为false。因为data值是FormData对象，不需要对数据做处理
		data : form,
		xhr : function() { //获取ajaxSettings中的xhr对象，为它的upload属性绑定progress事件的处理函数
			myXhr = $.ajaxSettings.xhr();
			if (progressFunction && myXhr.upload) { //检查进度函数和upload属性是否存在
				//绑定progress事件的回调函数
				myXhr.upload.addEventListener("progress",
						progressFunction, false);
			}
			return myXhr; //xhr对象返回给jQuery使用
		},
		error : function(request) {
			layer.msg("请求失败！", {icon: 2});
		},
		success : function(data) {
			//$("#btnUpload").val("上传完成");
			var jsonO = eval(data);
			if(jsonO[0].info =="error"){
				layer.msg("上传失败!");
				$("#btnUpload").attr("disabled", true);
				$("#progressbar").width(0);
				$("#progressbar").height(0);
				$("#progressbar").text("");
			}else{
				$("#filePath").val(jsonO[0].info);
				$("#progressbar").text("100% / 完成");
				getSheetName();
			}
		}
	});
}

//进度条控制
function progressFunction(evt) {
	if (evt.lengthComputable) {//lengthComputable，表示进度信息是否可用的布尔值；target，对应的 XHR 对象；
		var completePercent = Math.round(evt.loaded / evt.total*100);//loaded:已经接收的字节数；,total:响应数据的预期总字节数，根据 Content-Length 响应头部确定
		$("#progressbar").text(completePercent+"%");
		$("#progressbar").width((completePercent*3.7));
		$("#progressbar").height(34);
		//$("#btnUpload").val("进度");
		$("#btnUpload").hide();
	}
}

//选择excel--》 sheetname
function getSheetName(){
	$("#sheetName option").remove();
	var sourceType=$("#dataSourceType").val();
	//????
	var inPath=$("#filePath").val();
	$.ajax({
		type : "post",
		url : "./DataHandlingImportSheetName.do",
		data : {
			"inPath":inPath
		},
		dataType : "json",
		async : false,
		success : function(data) {
			var jsonO = eval(data);
			if(jsonO[0].info =="0"){
				layer.msg("没有获取到Sheet名称!", {icon: 5});
			}else{
				var dataRole = eval(jsonO[0].info);
				for(var i= 0; i< dataRole.length;i++){
					$("#sheetName").append('<option value="'+dataRole[i].sheetName+'">'+dataRole[i].sheetName+'</option>');
				}
			}
		},
		error : function(data) {
			layer.msg("请求失败！", {icon: 2});
		}
	});
}
function applicationUserAddBach(){
	var filePath = $("#filePath").val();
	var sheetName = $("#sheetName option:selected").val();
	
	if(filePath == ''){
		layer.msg("请上传文件！", {icon: 2});
		return;
	}
	if(sheetName == ''){
		layer.msg("请选择excel标签页sheet名称！", {icon: 2});
		return;
	}
	$("#content").hide();
	$("#loading").show();
	$.ajax({
		type : "post",
		url : "./applicationUserAddBatch.do",
		data : {
			"filePath":filePath,
			"sheetName":sheetName,
			"memberId":index1,
			"type":userType
		},
		dataType : "json",
		success : function(data) {
			var d = eval(data); 
			$("#content").show();
			$("#loading").hide();
			if(d[0].result == 'success'){
				var index = parent.layer.getFrameIndex(window.name); 
				if(userType == '2'){
					window.parent.window[winName].demo1(1);
				}else{
					window.parent.get();
				}
	        	parent.layer.msg("导入成功", {icon: 6});
	        	parent.layer.close(index);
			}else{
				arent.layer.msg("导入失败", {icon: 2});
			}
		},
		error : function(data) {
			$("#content").show();
			$("#loading").hide();
			layer.msg("请求失败！", {icon: 2});
		}
	});
}
</script>